<?php

namespace App\Http\Controllers\Admin\OrderManager;

use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Services\Tools\ExportService;
use Illuminate\Http\Request;    //引用http请求类
use App\Response\Response;

class NobilityController extends BaseController {


    /**
     * @api {post} /order/buy-nobility-list/  购买爵位订单-列表
     * @apiVersion 0.1.0
     * @apiName 购买爵位订单-列表
     * @apiGroup 订单管理
     *  @apiParam {number} nobility_id     爵位id，非必传
     * @apiParam {string} keyword      关键字：用户编号、昵称，非必传
     * @apiParam {number} page      页数(不传默认第一页)，非必传
     * @apiParam {number} perPage      每页数量，非必传
     * @apiSuccessExample 成功响应
    {
        "code": 200,
        "data": {
            "list": [
                {
                    "id(主键id)": 94,
                    "nobility_id（爵位id）": 3,
                    "money（购买金额）": 2400,
                    "add_time（购买时间）": "2020-08-19 18:00:36",
                    "expiration_time（过期时间）": "2020-09-18 18:00:36",
                    "user_code（用户编号）": 302928,
                    "nickname（用户昵称）": "胖头鱼。",
                    "head_picture（用户头像）": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/ios_images/2020-08-19/28A54104-7C98-48F7-BF7A-2ABB666E3F29.jpeg",
                    "nobility_name（爵位名字）": "侯爵",
                    "nobility_picture（爵位图片）": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/admin_images/5f3e283781369.png"
                }
            ],
            "meta": {
                "page": 1,
                "limit": 20,
                "next": null,
                "total": 1
            },
            "header": {
                "nobility_id（搜索爵位id）": "3",
                "keyword（搜索用户编号、昵称）": "胖"
            }
        }
    }
     */
    public  function  buyNobilityList(Request $request){
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'nobility_id' => 'filled|numeric',
                'keyword' => 'filled',
            ],
            [
                'keyword.filled' => '用户ID/昵称不能为空',
            ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $nobility_id = $request->input('nobility_id','');
        $keyword = $request->input('keyword','');
        $page = $request->input('page',1);
        $perPage = $request->input('perPage',20);

        $list = app('repo_nobility_buy')->buyNobilityList($nobility_id,$keyword,$page,$perPage);
        if ($list){
            foreach ($list as $key=>$value){
                $value->user_code = !empty($value->user_data->user_code) ? $value->user_data->user_code : 0;
                $value->nickname = !empty($value->user_data->nickname) ? $value->user_data->nickname : "";
                $value->head_picture = !empty($value->user_data->head_picture) ? $value->user_data->head_picture : "";
                $value->nobility_name = !empty($value->nobility->nobility_name) ? $value->nobility->nobility_name : "";
                $value->nobility_picture = !empty($value->nobility->picture) ? $value->nobility->picture : "";
                $value->add_time =date('Y-m-d H:i:s',$value->add_time);
                $value->expiration_time =date('Y-m-d H:i:s',$value->expiration_time);
                unset($value->user_id);
                unset($value->nobility);
                unset($value->user_data);
            }
        }
        $search = [
            'nobility_id'=>$nobility_id,
            'keyword'=>$keyword,
        ];
        return Response::sendList($list,[],$search);
    }

}
